package cn.stylefeng.guns.modular.comm.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.comm.util.CommConstant;
import cn.stylefeng.guns.comm.util.DictTypeConstant;
import cn.stylefeng.guns.modular.comm.entity.News;
import cn.stylefeng.guns.modular.comm.model.params.NewsParam;
import cn.stylefeng.guns.modular.comm.model.result.NewsResult;
import cn.stylefeng.guns.modular.comm.model.result.SwiperResult;
import cn.stylefeng.guns.modular.comm.service.NewsService;
import cn.stylefeng.guns.sys.core.properties.GunsProperties;
import cn.stylefeng.guns.sys.modular.system.entity.Dict;
import cn.stylefeng.guns.sys.modular.system.entity.FileInfo;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.guns.sys.modular.system.service.FileInfoService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 新闻通知控制器
 *
 * @author
 * @Date 2019-11-18 14:04:05
 */
@Controller
@RequestMapping("/news")
public class NewsController extends BaseController {

    private String PREFIX = "/comm/news";

    private final NewsService newsService;
    private final FileInfoService fileInfoService;
    private final GunsProperties gunsProperties;
    private final DictService dictService;

    public NewsController(DictService dictService, GunsProperties gunsProperties, FileInfoService fileInfoService, NewsService newsService) {
        this.dictService = dictService;
        this.gunsProperties = gunsProperties;
        this.fileInfoService = fileInfoService;
        this.newsService = newsService;
    }

    /**
     * 跳转到主页面
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("")
    public String index() {
        List<Dict> xwlx = dictService.listDictsByCode(DictTypeConstant.XWLX);
        setAttr("xwlx", xwlx);
        return PREFIX + "/news.html";
    }

    /**
     * 新增页面
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/news_add.html";
    }

    /**
     * 编辑页面
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/edit")
    public String edit() {
        List<Dict> xwlx = dictService.listDictsByCode(DictTypeConstant.XWLX);
        setAttr("xwlx", xwlx);
        return PREFIX + "/news_edit.html";
    }

    /**
     * 新增接口
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(NewsParam newsParam) {
        newsParam.setDelFlag(false);
        newsParam.setDisplayFlag("1");
        this.newsService.add(newsParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(NewsParam newsParam) {
        this.newsService.update(newsParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(NewsParam newsParam) {
        this.newsService.delete(newsParam);
        return ResponseData.success();
    }

    @RequestMapping("/change/display")
    @ResponseBody
    public ResponseData changeDisplay(NewsParam newsParam) {
        News detail = this.newsService.getById(newsParam.getId());
        if(detail.getDisplayFlag().equals("1")){
            newsParam.setDisplayFlag("0");
        }else{
            newsParam.setDisplayFlag("1");
        }
        this.newsService.update(newsParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author
     * @Date 2019-11-18
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(NewsParam newsParam) {
        News detail = this.newsService.getById(newsParam.getId());
        NewsResult swiperResult = new NewsResult();
        ToolUtil.copyProperties(detail, swiperResult);
        if (ToolUtil.isNotEmpty(detail.getFileId())) {
            FileInfo fileInfo = fileInfoService.getById(detail.getFileId());
            swiperResult.setFilePath(CommConstant.FILE_PATH + fileInfo.getFilePath().substring(gunsProperties.getFileUploadPath().length() - 1));
        }
        return ResponseData.success(swiperResult);
    }

    /**
     * 查询列表
     *
     * @author
     * @Date 2019-11-18
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(NewsParam newsParam) {
        return this.newsService.findPageBySpec(newsParam);
    }

}


